package com.ruoyi.bysj.controller;

import com.ruoyi.bysj.domain.SysAppointment;
import com.ruoyi.bysj.domain.SysOrder;
import com.ruoyi.bysj.domain.SysPay;
import com.ruoyi.bysj.domain.SysReception;
import com.ruoyi.bysj.service.ISysAppointmentService;
import com.ruoyi.bysj.service.ISysOrderService;
import com.ruoyi.bysj.service.ISysReceptionService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bysj/receptionist")
public class SysReceptionistController extends BaseController
{
    @Autowired
    private ISysAppointmentService sysAppointmentService;

    @Autowired
    private ISysOrderService sysOrderService;

    @Autowired
    private ISysReceptionService sysReceptionService;

    /**
     * 查询车主预约列表
     */
    @PreAuthorize("@ss.hasPermi('bysj:receptionist:appointment:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysAppointment sysAppointment)
    {
        startPage();
        sysAppointment.setAppointmentState("1");
        List<SysAppointment> list = sysAppointmentService.selectSysAppointmentList(sysAppointment);
        return getDataTable(list);
    }


    /**
     * 获取车主预约详细信息
     * @return
     */
    /**
     * 获取车主预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('bysj:receptionist:appointment:query')")
    @GetMapping(value = "/appointment/{appointmentId}")
    public AjaxResult getInfo(@PathVariable("appointmentId") Long appointmentId)
    {
        return AjaxResult.success(sysAppointmentService.selectSysAppointmentByAppointmentId(appointmentId));
    }

    @PreAuthorize("@ss.hasPermi('bysj:receptionist:order:query')")
    @GetMapping(value = "/order")
    public TableDataInfo order(SysOrder sysOrder)
    {
        sysOrder.setOrderState("1");
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('bysj:receptionist:order:list')")
    @GetMapping(value = "/orderList")
    public TableDataInfo order2(SysOrder sysOrder)
    {
        startPage();
        sysOrder.setOrderState("4");
        List<SysOrder> list = sysOrderService.selectSysOrderList(sysOrder);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('bysj:receptionist:order:query')")
    @GetMapping(value = "/order/{orderId}")
    public AjaxResult getInfo2(@PathVariable("orderId") Long orderId)
    {
        return AjaxResult.success(sysOrderService.selectSysOrderByOrderId(orderId));
    }

    @PreAuthorize("@ss.hasPermi('bysj:receptionist:add')")
    @Log(title = "车主预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysAppointment sysAppointment, SysOrder sysOrder)
    {
        sysOrder.setAppointmentId(sysAppointment.getAppointmentId());
        sysOrder.setAppointmentBusiness(sysAppointment.getAppointmentBusiness());
        sysOrder.setAppointmentCar(sysAppointment.getAppointmentCar());
        sysOrder.setAppointmentName(sysAppointment.getAppointmentName());
        sysOrder.setAppointmentProblem(sysAppointment.getAppointmentProblem());
        sysOrder.setOrderState("1");
        sysOrderService.insertSysOrder(sysOrder);
        sysAppointment.setAppointmentState("2");
        return toAjax( sysAppointmentService.updateSysAppointment(sysAppointment));
    }

    @PreAuthorize("@ss.hasPermi('bysj:receptionist:receptionadd')")
    @Log(title = "取车登记", businessType = BusinessType.INSERT)
    @PostMapping("/receptionadd")
    public AjaxResult add(@RequestBody SysReception sysReception)
    {
        sysReception.setReceptionName(getUsername());
        sysReception.setReceptionBusiness("2");
        return toAjax(sysReceptionService.insertSysReception(sysReception));
    }

    @PreAuthorize("@ss.hasPermi('bysj:receptionist:pay')")
    @Log(title = "支付订单", businessType = BusinessType.UPDATE)
    @PutMapping("/pay")
    public AjaxResult pay(@RequestBody SysPay sysPay)
    {
        sysPay.setPayName(getUsername());
        return toAjax(sysOrderService.updateSysPay(sysPay));
    }

}
